import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base/constant/Constants.dart';

import 'package:flutter_base/resource/theme/theme_enum.dart';
import 'package:flutter_base/resource/theme/theme_ui.dart';
import 'package:provider/provider.dart';

class DarkModeProvider with ChangeNotifier {
  /// 深色模式 0: 关闭 1: 开启 2: 随系统
  int _darkMode;

  int get darkMode => _darkMode;

  bool isDarkMode() {
    return WidgetsBinding.instance.window.platformBrightness == Brightness.dark;
  }

  void changeMode(int darkMode) async {
    _darkMode = darkMode;

    if (darkMode == ThemeEnum.system.index) {
      if (isDarkMode()) {
        getTheme(ThemeEnum.dark.index);
        print("theme-dark");
      } else {
        getTheme(ThemeEnum.light.index);
        print("theme-light");
      }
    } else {
      getTheme(darkMode);
    }

    SpUtil.putInt(Constants.DARK_MODE, darkMode);
    notifyListeners();
  }
}

setTheme(BuildContext context, {ThemeEnum themeEnum}) {
  if (themeEnum == null) {
    Provider.of<DarkModeProvider>(context, listen: false).changeMode(
        SpUtil.getInt(Constants.DARK_MODE, defValue: ThemeEnum.system.index));
  } else {
    Provider.of<DarkModeProvider>(context, listen: false)
        .changeMode(themeEnum.index);
  }
}
